package ru.ipccenter.managedBeans;

import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;


/**
 * MIPT
 * Autor: Nikolai
 * Date: 27.04.14
 */
@SessionScoped
@ManagedBean(name = "login")
public class LoginManagedBean implements Serializable {
    
    private static final Logger log = Logger.getLogger(LoginManagedBean.class.getName());
    private String login;
    private String password;

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public void doLogin() {
        
        UsernamePasswordToken token = new UsernamePasswordToken(login, password);
        token.setRememberMe(true);
        
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.login(token);
        
        try {
            currentUser.login(token);
        } catch (UnknownAccountException uae) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                                                         "Login&Password is failed", "Couldn't find such account"));
        } catch (IncorrectCredentialsException ice) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                                                         "Login/Password is failed", "False combination of login+password"));
        } catch (LockedAccountException lae) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                                                         "Your account is locked", "Write to admin for more info"));
        } catch (ExcessiveAttemptsException eae) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                                                         "Too much attempts at the time", "Please, try later"));
        } catch (AuthenticationException ae) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                                                         "Unexpected error", "Please, try later"));
        }
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        } catch (IOException ioe) {
            log.log(Level.SEVERE, null, ioe);
        }
    }
    
    public void doRegister() {
        // NOT ENOUGH TIME TO DO IT
    }
}
